package com.neuedu.yyzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.entity.NurseContent;
import com.neuedu.yyzx.entity.Permission;
import com.neuedu.yyzx.entity.RolePermission;
import com.neuedu.yyzx.entity.UserRole;
import com.neuedu.yyzx.mapper.PermissionMapper;
import com.neuedu.yyzx.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.yyzx.service.IRolePermissionService;
import com.neuedu.yyzx.service.IUserRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author dya
 * @since 2021-05-12
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Resource
    IUserRoleService userRoleService;

    @Resource
    IPermissionService permissionService;
    @Resource
    IRolePermissionService rolePermissionService;


    @Override
    public Object list(Permission permission) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",1111);
        IPage<Permission> page = this.page(new Page<>(permission.getPageNo(),permission.getPageSize()),wrapper);
        List<Permission> list = page.getRecords();
        for(Permission permission1 : list) {

            getChildren(permission1);
        }
        return page;
    }

    @Override
    public boolean updateAll(Permission permission) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("name", permission.getName());
        updateWrapper.set("url", permission.getUrl());
        updateWrapper.set("type", permission.getType());
        updateWrapper.set("parent_id", permission.getParentId());
        updateWrapper.eq("id", permission.getId());
        int success = getBaseMapper().update(permission, updateWrapper);

        return success>0;
    }




    /**
     * 参数为父权限对象
     * 通过父权限获取子权限
     *
     * */

    public void getChildren(Permission permission) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",permission.getId());
        List<Permission> list = this.list(wrapper);

        permission.setChildren(list);
    }


    @Override
    public List<Permission> userPermissionList(Integer userId) {
        //1根据用户id查询出所有的角色ums_user_role
        QueryWrapper<UserRole> umsUserQueryWrapper = new QueryWrapper<>();
        umsUserQueryWrapper.eq("user_id",userId);
        UserRole userRole = userRoleService.getOne(umsUserQueryWrapper);
        if(userRole==null || userRole.getRoleId() == null){
            return null;
        }

        //System.out.println(umsUserRole.getRoleId()); //
        String[] roleIds = userRole.getRoleId().split(",");
        //根据角色获取权限id集合
        QueryWrapper<RolePermission> rolePermissionWrapper = new QueryWrapper<>();
       rolePermissionWrapper.in("role_id",roleIds);
        List<RolePermission> rolePermissionList = rolePermissionService.list(rolePermissionWrapper);
        if(rolePermissionList ==null ){
            return null;
        }

        Set permissionIdSet = new HashSet();
        for (RolePermission rolePermission : rolePermissionList) {
            permissionIdSet.addAll(Arrays.asList(rolePermission.getPermissioinId().split(",")));
        }

//        permissionIdSet.forEach(System.out::println);

        //根据权限id查询权限列表
        QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
        permissionQueryWrapper.in("id",permissionIdSet).or().in("parent_id",permissionIdSet);
        List<Permission> allPermission = this.list(permissionQueryWrapper);
        //目标，将顶层菜单放入
        List<Permission> frontPermissionList = new ArrayList();
        for (Permission permission : allPermission) {
            if(permission.getParentId() == 1111){
                setChildrenPermission(permission,allPermission);
                frontPermissionList.add(permission);
            }
        }
        return frontPermissionList;
    }

    private void setChildrenPermission(Permission permission, List<Permission> allPermission) {
        List<Permission> children = new ArrayList();
        for (Permission permission2 : allPermission) {
            if(permission.getId() == permission2.getParentId()){
                children.add(permission2);
            }
        }
        permission.setChildren(children);

    }



    @Override
    public List<Permission> userPermissionListValidate(Integer userId) {
        //1根据用户id查询出所有的角色ums_user_role
        QueryWrapper<UserRole> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_id",userId);
        UserRole umsUserRole = userRoleService.getOne(userQueryWrapper);
        if(umsUserRole==null || umsUserRole.getRoleId() == null){
            return null;
        }

        //System.out.println(umsUserRole.getRoleId()); //
        String[] roleIds = umsUserRole.getRoleId().split(",");
        //根据角色获取权限id集合
        QueryWrapper<RolePermission>rolePermissionWrapper = new QueryWrapper<>();
        rolePermissionWrapper.in("role_id",roleIds);
        List<RolePermission> rolePermissionList = rolePermissionService.list(rolePermissionWrapper);
        if(rolePermissionList ==null ){
            return null;
        }

        Set permissionIdSet = new HashSet();
        for (RolePermission rolePermission : rolePermissionList) {
            permissionIdSet.addAll(Arrays.asList(rolePermission.getPermissioinId().split(",")));
        }

        //根据权限id查询权限列表
        QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
        permissionQueryWrapper.in("id",permissionIdSet);
        List<Permission> allPermission = this.list(permissionQueryWrapper);
        return allPermission;
    }




}
